
class Node(object):
    '''
    节点类
    '''
    def __init__(self, item):
        self.item = item
        self.next = None


class SingleCycleLinkList(object):
    '''
    单链循环表类
    '''
    def __init__(self, node=None):
        self.__head = node
        if node:
            node.next = node

    def is_empty(self):
        '''链表是否为空'''
        return self.__head is None

    def length(self):
        '''链表的长度'''
        # cur游标，用来移动遍历节点
        cur = self.__head
        # count记录数量
        if self.is_empty():
            return 0
        count = 1
        while cur.next != self.__head:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        '''遍历整个链表'''
        if self.is_empty():
            return
        cur = self.__head
        while cur.next != self.__head:
            print(cur.item, end=' ')
            cur = cur.next
        print(cur.item, end=' ')
        print(end='\n')

    def append(self, item):
        '''链表尾部添加元素'''
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
            cur.next = node
            node.next = self.__head

    def add(self, item):
        '''链表头部添加元素'''
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            if self.length() == 1:
                cur = self.__head
                self.__head = node
                node.next = cur
                cur.next = node
            else:
                cur = self.__head
                while cur.next != self.__head:
                    cur = cur.next
                node.next = self.__head
                self.__head = node
                cur.next = self.__head

    def insert(self, index, item):
        '''插入元素'''
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
        elif index <= 0:
            self.add(item)
        elif index >= self.length():
            self.append(item)
        else:
            cur = self.__head
            count = 0
            while count < index-1:
                count += 1
                cur = cur.next
            node.next = cur.next
            cur.next = node

    def search(self, item):
        '''查找元素'''
        cur = self.__head
        while cur.next != self.__head:
            if cur.item == item:
                return True
            else:
                cur = cur.next
        if cur.item == item:
            return True
        return False

    def remove(self, item):
        '''删除元素'''
        cur = self.__head
        pre = None
        while cur.next != self.__head:
            if cur.item == item:
                # 先判断此节点是否是头节点
                if cur == self.__head:
                    remove_node = cur
                    while cur.next != self.__head:
                        cur = cur.next
                    cur.next = self.__head.next
                    self.__head = remove_node.next
                else:
                    pre.next = cur.next
                return item
            else:
                pre = cur
                cur = cur.next
        if cur.item == item:
            pre.next = self.__head
            return item


if __name__ == '__main__':
    ll = SingleCycleLinkList()
    print(ll.is_empty())
    print(ll.length())

    ll.append(1)
    ll.append(13)
    ll.append(16)
    ll.append(35)
    ll.append(2)
    ll.add(1000)
    ll.insert(7, 50)
    print(ll.is_empty())
    print(ll.length())
    ll.travel()
    print(ll.search(1000))
    ll.remove(1000)
    ll.travel()


